package lyx.tank.logic
{
	import lyx.game.element.GameElement;
	import lyx.tank.element.AITank;
	import lyx.tank.element.Bullet;
	import lyx.tank.element.Tank;
	import lyx.game.utils.ILogic;

	public class TankLogic implements ILogic
	{
		public function handle(interval:int,element:GameElement,game_elements:Array):void
		{
			if(element.life==0)
			{
				return;
			}
			
			var tank:Tank = element as Tank;
			
			if(tank is AITank)
			{
				if(tank.steps==0)
				{
					tank.steps = Math.random()*50;
					tank.direction = int(Math.random()*4)*2;
				}
				var attack:int = Math.random()*50;
				if(attack<3)
				{
					var bullet:Bullet = Tank(element).fire();
					if(bullet!=null)
					{
						game_elements.push(bullet);
					}
				}
			}
			
			tank.move(interval);
			
			for each(var item:GameElement in game_elements)
			{
				if(tank==item || item.life==0)
				{
					continue;
				}
				if((item is Bullet))
				{
					continue;
				}
				
				var is_hit:Boolean = tank.hitTest(item);
				if(is_hit)
				{
					tank.moveRollBack();
				}
				
			}
		}
	}
}